package com.ivsign.lib_idcardidentification;

import android.bluetooth.BluetoothGatt;
import android.content.Context;
import android.util.Log;

import com.clj.fastble.BleManager;
import com.clj.fastble.callback.BleGattCallback;
import com.clj.fastble.callback.BleWriteCallback;
import com.clj.fastble.data.BleDevice;
import com.clj.fastble.exception.BleException;
import com.clj.fastble.utils.BleLog;

/**
 * @description :
 * @autHor :  Jason
 * @date : 2018/8/17 17:13
 */

public class IDCardCommunication {

    private static final String LOGTAG = "IDCardCommunication";

    private BleDevice bleDevice;
    private DeviceUUID deviceUUID;
    private BleWriteCallback mWriteRsp;
    private Context context;

    public IDCardCommunication(BleDevice bleDevice, DeviceUUID deviceUUID, BleWriteCallback bleWriteCallback, Context context) {
        this.bleDevice = bleDevice;
        this.deviceUUID = deviceUUID;
        this.mWriteRsp = bleWriteCallback;
        this.context = context;
    }

    /*   02000250005003
     * 发送5000寻卡至蓝牙读卡器 ble
     */
    public void sendcardtran5000ByBle() {
        byte[] bluecmd = new byte[7];
        bluecmd[0] = 0x02;
        bluecmd[1] = 0;
        bluecmd[2] = 2;
        bluecmd[3] = 0x50;
        bluecmd[4] = 0;
        bluecmd[5] = 0x50;
        bluecmd[6] = 03;
        ///checkAndOpenBLE();
        BleManager.getInstance().write(
                bleDevice,
                deviceUUID.getService(),
                deviceUUID.getWriteChracter(),
                bluecmd,
                mWriteRsp);
        BleLog.e("send" + "02000250005003");
    }

    private void checkAndOpenBLE() {
        if (!BleManager.getInstance().isBlueEnable()) {
            BleManager.getInstance().enableBluetooth();

        }
        if (!BleManager.getInstance().isConnected(bleDevice)) {
            //Toast.makeText(context,"蓝牙设备连接失败!"+exception.getDescription(),Toast.LENGTH_SHORT).show();
            BleManager.getInstance().connect(bleDevice.getMac(), new BleGattCallback() {
                @Override
                public void onStartConnect() {

                }

                @Override
                public void onConnectFail(BleDevice bleDevice, BleException exception) {
                }

                @Override
                public void onConnectSuccess(BleDevice bleDevice, BluetoothGatt gatt, int status) {

                }

                @Override
                public void onDisConnected(boolean isActiveDisConnected, BleDevice device, BluetoothGatt gatt, int status) {

                }
            });
            return;
        }
    }

    /*
     * 回送5001数据至蓝牙读卡器
     */
    public void sendcardtran5001ByBle(byte[] cmd, long wait) {

        byte[] subcmd = new byte[cmd.length + 2];
        subcmd[0] = 0x50;
        subcmd[1] = 1;
        System.arraycopy(cmd, 0, subcmd, 2, cmd.length);

        byte[] bluecmd = new byte[subcmd.length + 5];
        bluecmd[0] = 0x02;
        byte[] len = Util.intTo2byte(subcmd.length);
        bluecmd[1] = len[0];
        bluecmd[2] = len[1];
        int cmdlen = subcmd.length;
        System.arraycopy(subcmd, 0, bluecmd, 3, cmdlen);

        bluecmd[cmdlen + 1] = -112; // 0x90
        bluecmd[cmdlen + 2] = 0x00;
        bluecmd[cmdlen + 4] = 0x03;
        bluecmd[cmdlen + 3] = CheckUtils.Verify_02head_03end(bluecmd);
        BleManager.getInstance().write(
                bleDevice,
                deviceUUID.getService(),
                deviceUUID.getWriteChracter(),
                bluecmd,
                mWriteRsp);
        Log.e(LOGTAG, "sendtonfc=" + Util.bytesToHexString(bluecmd));

    }

    /*
     * 回送5012数据至蓝牙读卡器
     */
    public void sendcardtran5012ByBle(byte[] cmd) {

        byte[] subcmd = new byte[cmd.length];
        System.arraycopy(cmd, 0, subcmd, 0, cmd.length);

        byte[] bluecmd = new byte[subcmd.length + 5];
        bluecmd[0] = 0x02;
        byte[] len = Util.intTo2byte(subcmd.length);
        bluecmd[1] = len[0];
        bluecmd[2] = len[1];
        int cmdlen = subcmd.length;
        System.arraycopy(subcmd, 0, bluecmd, 3, cmdlen);

        bluecmd[cmdlen + 4] = 0x03;
        bluecmd[cmdlen + 3] = CheckUtils.Verify_02head_03end(bluecmd);
        BleManager.getInstance().write(
                bleDevice,
                deviceUUID.getService(),
                deviceUUID.getWriteChracter(),
                bluecmd,
                mWriteRsp);

        Log.e(LOGTAG, "sendtonfc=" + Util.bytesToHexString(bluecmd));
    }

    /*
     * 回送5022数据至蓝牙读卡器
     */
    public void sendcardtran5022ByBle(byte[] cmd) {
        byte[] subcmd = new byte[cmd.length];
        System.arraycopy(cmd, 0, subcmd, 0, cmd.length);

        byte[] bluecmd = new byte[subcmd.length + 5];
        bluecmd[0] = 0x02;
        byte[] len = Util.intTo2byte(subcmd.length);
        bluecmd[1] = len[0];
        bluecmd[2] = len[1];
        int cmdlen = subcmd.length;
        System.arraycopy(subcmd, 0, bluecmd, 3, cmdlen);

        bluecmd[cmdlen + 1] = -112; // 0x90
        bluecmd[cmdlen + 2] = 0x00;
        bluecmd[cmdlen + 4] = 0x03;
        bluecmd[cmdlen + 3] = CheckUtils.Verify_02head_03end(bluecmd);
        BleManager.getInstance().write(
                bleDevice,
                deviceUUID.getService(),
                deviceUUID.getWriteChracter(),
                bluecmd,
                mWriteRsp);
        Log.e(LOGTAG, "sendtonfc=" + Util.bytesToHexString(bluecmd));

        Log.e(LOGTAG, "===================run3333333333===");
        // read5022run3();
    }
}
